﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="{x:Type Menu}">
        <Setter Property="FontFamily" Value="Tahoma" />
        <Setter Property="FontSize" Value="11" />
        <Setter Property="FontStyle" Value="{DynamicResource {x:Static SystemFonts.MenuFontStyleKey}}" />
        <Setter Property="FontWeight" Value="{DynamicResource {x:Static SystemFonts.MenuFontWeightKey}}" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Margin" Value="0 0 0 5" />
        <Setter Property="Padding" Value="3" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="True" Background="{DynamicResource MenuBackground}">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- MENU ITEM -->
    <PathGeometry x:Key="Checkmark" Figures="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" />
    <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />
    <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
    <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
    <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>

    <Style x:Key="MenuScrollButton" TargetType="{x:Type RepeatButton}">
        <Setter Property="ClickMode" Value="Hover" />
        <Setter Property="MinWidth" Value="0" />
        <Setter Property="MinHeight" Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RepeatButton}">
                    <Border x:Name="ClassicBorder" Background="{TemplateBinding Background}" BorderBrush="#888888"
                            BorderThickness="1">
                        <ContentPresenter HorizontalAlignment="Center" Margin="1" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="BorderThickness" TargetName="ClassicBorder" Value="2" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}"
            TargetType="{x:Type ScrollViewer}">
        <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
        <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ScrollViewer}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Border Grid.Column="0" Grid.Row="1">
                            <ScrollContentPresenter Margin="{TemplateBinding Padding}" />
                        </Border>
                        <RepeatButton Focusable="false" Style="{StaticResource MenuScrollButton}"
                                Command="{x:Static ScrollBar.LineUpCommand}"
                                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                Grid.Column="0" Grid.Row="0">
                            <RepeatButton.Visibility>
                                <MultiBinding FallbackValue="Visibility.Collapsed"
                                        Converter="{StaticResource MenuScrollingVisibilityConverter}"
                                        ConverterParameter="0">
                                    <Binding Path="ComputedVerticalScrollBarVisibility"
                                            RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                                </MultiBinding>
                            </RepeatButton.Visibility>
                            <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"
                                    Data="{StaticResource UpArrow}" />
                        </RepeatButton>
                        <RepeatButton Focusable="false" Style="{StaticResource MenuScrollButton}"
                                Command="{x:Static ScrollBar.LineDownCommand}"
                                CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                Grid.Column="0" Grid.Row="2">
                            <RepeatButton.Visibility>
                                <MultiBinding FallbackValue="Visibility.Collapsed"
                                        Converter="{StaticResource MenuScrollingVisibilityConverter}"
                                        ConverterParameter="100">
                                    <Binding Path="ComputedVerticalScrollBarVisibility"
                                            RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="VerticalOffset" RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="ExtentHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                                    <Binding Path="ViewportHeight" RelativeSource="{RelativeSource TemplatedParent}" />
                                </MultiBinding>
                            </RepeatButton.Visibility>
                            <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"
                                    Data="{StaticResource DownArrow}" />
                        </RepeatButton>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate
            x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}"
            TargetType="{x:Type MenuItem}">
        <Border x:Name="MainBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
                SnapsToDevicePixels="True" CornerRadius="1">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="MenuItemIconColumnGroup" MinWidth="17" Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto" />
                    <ColumnDefinition Width="14" />
                </Grid.ColumnDefinitions>
                <ContentPresenter Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon" />
                <Path Visibility="Hidden" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight"
                        Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center"
                        Data="{StaticResource Checkmark}" />
                <ContentPresenter x:Name="HeaderPresenter" Margin="{TemplateBinding Padding}" Grid.Column="1"
                        ContentSource="Header" RecognizesAccessKey="True" />
                <Path Fill="{TemplateBinding Foreground}" Margin="4,0,6,0" x:Name="ArrowPanel"
                        VerticalAlignment="Center" Data="{StaticResource RightArrow}" Grid.Column="3"
                        DockPanel.Dock="Right" />
                <Popup Focusable="false" AllowsTransparency="true"
                        IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
                        Placement="Right"
                        PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                        VerticalOffset="-2" x:Name="PART_Popup">
                    <Border x:Name="SubMenuBorder" Background="{DynamicResource MenuItemBackground}"
                            BorderBrush="{DynamicResource MenuBorderBrush}" BorderThickness="1"
                            SnapsToDevicePixels="True">
                        <ScrollViewer
                                Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"
                                CanContentScroll="true">
                            <ItemsPresenter Grid.IsSharedSizeScope="true"
                                    KeyboardNavigation.DirectionalNavigation="Cycle"
                                    KeyboardNavigation.TabNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource SubMenuItemForeground}" />
            </Trigger>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible" />
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource SubMenuItemHotBackground}" />
                <Setter Property="BorderBrush" TargetName="MainBorder"
                        Value="{DynamicResource SubMenuItemHotBorderBrush}" />
                <Setter Property="Foreground" Value="{DynamicResource SubMenuItemHotForeground}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource MenuItemDisabledForeground}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate
            x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}"
            TargetType="{x:Type MenuItem}">
        <Border x:Name="MainBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
                SnapsToDevicePixels="True" CornerRadius="1">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition SharedSizeGroup="MenuItemIconColumnGroup" MinWidth="17" Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto" />
                    <ColumnDefinition Width="14" />
                </Grid.ColumnDefinitions>
                <ContentPresenter Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon" />
                <Path Visibility="Hidden" Fill="{TemplateBinding Control.Foreground}" FlowDirection="LeftToRight"
                        Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center"
                        Data="{StaticResource Checkmark}" />
                <ContentPresenter x:Name="HeaderPresenter" Margin="{TemplateBinding Padding}" Grid.Column="1"
                        ContentSource="Header" RecognizesAccessKey="True" />
                <TextBlock Margin="5,2,0,2" x:Name="InputGestureText" Grid.Column="2" DockPanel.Dock="Right"
                        Text="{TemplateBinding InputGestureText}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource SubMenuItemForeground}" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible" />
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource SubMenuItemHotBackground}" />
                <Setter Property="BorderBrush" TargetName="MainBorder"
                        Value="{DynamicResource SubMenuItemHotBorderBrush}" />
                <Setter Property="Foreground" Value="{DynamicResource SubMenuItemHotForeground}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource MenuItemDisabledForeground}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate
            x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}"
            TargetType="{x:Type MenuItem}">
        <Border x:Name="MainBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
                CornerRadius="1" SnapsToDevicePixels="True">
            <DockPanel>
                <ContentPresenter Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon" />
                <Path Visibility="Collapsed" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight"
                        Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center"
                        Data="{StaticResource Checkmark}" />
                <ContentPresenter x:Name="HeaderPresenter" Margin="{TemplateBinding Padding}" ContentSource="Header"
                        RecognizesAccessKey="True" />
                <Popup Focusable="false" AllowsTransparency="true" HorizontalOffset="-1"
                        IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
                        Placement="Bottom"
                        PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
                        x:Name="PART_Popup">
                    <Border x:Name="SubMenuBorder" Background="{DynamicResource MenuItemBackground}"
                            BorderBrush="#EEEEEE" BorderThickness="1" SnapsToDevicePixels="True">
                        <ScrollViewer
                                Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"
                                CanContentScroll="true">
                            <ItemsPresenter Grid.IsSharedSizeScope="true"
                                    KeyboardNavigation.DirectionalNavigation="Cycle"
                                    KeyboardNavigation.TabNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>
            </DockPanel>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource TopMenuItemForeground}" />
            </Trigger>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter Property="PopupAnimation" TargetName="PART_Popup" Value="None" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible" />
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource MenuItemHotBackground}" />
                <Setter Property="BorderBrush" TargetName="MainBorder" Value="{DynamicResource MenuItemHotBackground}" />
                <Setter Property="Foreground" Value="{DynamicResource TopMenuItemHotForeground}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource MenuItemBackground}" />
            </Trigger>
            <Trigger Property="IsSubmenuOpen" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource MenuItemBackground}" />
                <Setter Property="BorderBrush" TargetName="MainBorder" Value="{DynamicResource MenuBorderBrush}" />
                <Setter Property="Foreground" Value="{DynamicResource TopMenuItemHotForeground}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource MenuItemDisabledForeground}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate
            x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}"
            TargetType="{x:Type MenuItem}">
        <Border x:Name="MainBorder" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
                CornerRadius="1" SnapsToDevicePixels="True">
            <DockPanel x:Name="ContentPanel">
                <ContentPresenter Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon" />
                <Path Visibility="Collapsed" Fill="{TemplateBinding Foreground}" FlowDirection="LeftToRight"
                        Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center"
                        Data="{StaticResource Checkmark}" />
                <ContentPresenter x:Name="HeaderPresenter" Margin="{TemplateBinding Padding}" ContentSource="Header"
                        RecognizesAccessKey="True" />
            </DockPanel>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsEnabled" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource TopMenuItemForeground}" />
            </Trigger>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible" />
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource MenuItemBackground}" />
                <Setter Property="BorderBrush" TargetName="MainBorder" Value="{DynamicResource MenuBorderBrush}" />
                <Setter Property="Foreground" Value="{DynamicResource TopMenuItemHotForeground}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="true">
                <Setter Property="Background" TargetName="MainBorder" Value="{DynamicResource MenuItemBackground}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource MenuItemDisabledForeground}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="{x:Type MenuItem}">
        <Setter Property="HorizontalContentAlignment"
                Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="VerticalContentAlignment"
                Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template"
                Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
        <Style.Triggers>
            <Trigger Property="Role" Value="TopLevelHeader">
                <Setter Property="Margin" Value="0,1,0,1" />
                <Setter Property="Padding" Value="4,1,5,2" />
                <Setter Property="Template"
                        Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
            </Trigger>
            <Trigger Property="Role" Value="TopLevelItem">
                <Setter Property="Margin" Value="0,1,0,1" />
                <Setter Property="Padding" Value="4,1,5,2" />
                <Setter Property="Template"
                        Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
            </Trigger>
            <Trigger Property="Role" Value="SubmenuHeader">
                <Setter Property="DockPanel.Dock" Value="Top" />
                <Setter Property="Padding" Value="0,2,0,2" />
                <Setter Property="Template"
                        Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
            </Trigger>
            <Trigger Property="Role" Value="SubmenuItem">
                <Setter Property="DockPanel.Dock" Value="Top" />
                <Setter Property="Padding" Value="0,2,0,2" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Grid SnapsToDevicePixels="true" Margin="0,0,0,0">
                        <Rectangle Height="1" Margin="0,0,0,0" Fill="{DynamicResource MenuBorderBrush}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</ResourceDictionary>